"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TableWebLayout = void 0;

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _configContext = require("../../../extends/configContext");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var TableWebLayout = /*#__PURE__*/function (_Component) {
  _inherits(TableWebLayout, _Component);

  var _super = _createSuper(TableWebLayout);

  function TableWebLayout(props) {
    var _this;

    _classCallCheck(this, TableWebLayout);

    _this = _super.call(this, props);

    _defineProperty(_assertThisInitialized(_this), "tableRef", /*#__PURE__*/(0, _react.createRef)());

    _defineProperty(_assertThisInitialized(_this), "computeWidth", function (columns) {
      var containerWidth = _this.tableRef.current ? _this.tableRef.current.offsetWidth : 0;
      var knownWidth = 0; // 有配置的width总和

      var noWidthNum = 0; // 没有配置width的列数量

      var hasWidth = function hasWidth(w) {
        return w && (_this.isNumber(w) || _this.isPercent(w));
      };

      columns.forEach(function (citem) {
        var cwidth = citem.width;

        if (hasWidth(cwidth)) {
          if (_this.isNumber(cwidth)) {
            citem.width = cwidth * 1;
            knownWidth += cwidth * 1;
          }

          if (_this.isPercent(cwidth)) {
            var _cwidth = containerWidth * Number(cwidth.substring(0, cwidth.length - 1)) / 100;

            citem.width = _cwidth * 1;
            knownWidth += _cwidth;
          }
        } else {
          noWidthNum++;
        }
      });
      var remainWidth = containerWidth - knownWidth;
      columns.forEach(function (citem) {
        if (!hasWidth(citem.width)) {
          var remainItemWidth = remainWidth / noWidthNum;
          var minWidth = 100;
          citem.width = remainItemWidth < minWidth ? minWidth : remainItemWidth;
        }
      });
      return columns;
    });

    _defineProperty(_assertThisInitialized(_this), "computeFix", function (_columns) {
      var fixedleft_columns = _columns.filter(function (i) {
        return i.fixed === 'left';
      });

      var fixedright_columns = _columns.filter(function (i) {
        return i.fixed === 'right';
      });

      var nofixed_columns = _columns.filter(function (i) {
        return !i.fixed;
      });

      fixedleft_columns.forEach(function (item, index) {
        var _fixedleft_columns;

        item.fixedLength = ((_fixedleft_columns = fixedleft_columns[index - 1]) === null || _fixedleft_columns === void 0 ? void 0 : _fixedleft_columns.width) || 0;
      });
      fixedright_columns.forEach(function (item, index) {
        var _fixedright_columns;

        item.fixedLength = ((_fixedright_columns = fixedright_columns[index + 1]) === null || _fixedright_columns === void 0 ? void 0 : _fixedright_columns.width) || 0;
      });
      var return_columns = fixedleft_columns.concat(nofixed_columns).concat(fixedright_columns);
      return return_columns;
    });

    _defineProperty(_assertThisInitialized(_this), "isNumber", function (n) {
      return /^[0-9]+.?[0-9]*$/.test(n);
    });

    _defineProperty(_assertThisInitialized(_this), "isPercent", function (str) {
      if (typeof str === 'string') {
        var symbol = str.charAt(str.length - 1);

        if (symbol == '%') {
          var subS = str.substring(0, str.length - 1);

          if (isNaN(Number(subS))) {
            return false;
          }

          return true;
        }
      }

      return false;
    });

    _defineProperty(_assertThisInitialized(_this), "renderTd", function (recordItem, columnsItem, index) {
      if (columnsItem.render && typeof columnsItem.render === 'function') {
        return columnsItem.render(recordItem, columnsItem, index);
      }

      return recordItem[columnsItem.key];
    });

    _defineProperty(_assertThisInitialized(_this), "initTrStyle", function (columnsItem) {
      var style = {
        width: columnsItem.width
      };

      if (columnsItem.fixed && columnsItem.fixed === 'left') {
        style.position = 'sticky';
        style.left = columnsItem.fixedLength;
      }

      if (columnsItem.fixed && columnsItem.fixed === 'right') {
        style.position = 'sticky';
        style.right = columnsItem.fixedLength;
      }

      return style;
    });

    _this.state = {
      columns: []
    };
    return _this;
  }

  _createClass(TableWebLayout, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      console.dir(this.tableRef.current);
      this.setState({
        columns: this.computeFix(this.computeWidth(this.props.columns || []))
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var clsPrefix = this.context.clsPrefix;
      var columns = this.state.columns;
      var _this$props = this.props,
          maxScrollHeight = _this$props.maxScrollHeight,
          dataSource = _this$props.dataSource;
      var allWidth = 0;
      columns.forEach(function (i) {
        allWidth += i.width;
      });
      return /*#__PURE__*/_react.default.createElement("div", {
        ref: this.tableRef,
        className: (0, _classnames.default)("".concat(this.props.className || ''), "".concat(clsPrefix, "-table")),
        style: {
          maxHeight: maxScrollHeight ? maxScrollHeight : 'none'
        }
      }, /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(clsPrefix, "-table__thead")
      }, /*#__PURE__*/_react.default.createElement("div", {
        style: {
          width: allWidth
        },
        className: "".concat(clsPrefix, "-table__tr")
      }, columns.map(function (columnsItem) {
        return /*#__PURE__*/_react.default.createElement("div", {
          style: _this2.initTrStyle(columnsItem),
          className: "".concat(clsPrefix, "-table__thead--th")
        }, columnsItem.name);
      }))), /*#__PURE__*/_react.default.createElement("div", {
        className: "".concat(clsPrefix, "-table__tbody")
      }, (dataSource || []).map(function (recordItem, index) {
        return /*#__PURE__*/_react.default.createElement("div", {
          style: {
            width: allWidth
          },
          className: "".concat(clsPrefix, "-table__tr")
        }, columns.map(function (columnsItem) {
          return /*#__PURE__*/_react.default.createElement("div", {
            style: _this2.initTrStyle(columnsItem),
            className: "".concat(clsPrefix, "-table__tbody--td")
          }, _this2.renderTd(recordItem, columnsItem, index));
        }));
      })));
    }
  }]);

  return TableWebLayout;
}(_react.Component);

exports.TableWebLayout = TableWebLayout;

_defineProperty(TableWebLayout, "contextType", _configContext.ConfigContext);